#include "imageprovide.h"
#include <qdebug.h>
#include <QTimer>
#include <QRegion>

ImageProvide::ImageProvide(QQuickItem *parent) : QQuickPaintedItem(parent)
{
    imageWrapper.setParent(this);
}

void ImageProvide::initImage( QString path )
{
    m_imageThumb = QImage(path);
    setNewSize(m_imageThumb.width(), m_imageThumb.height());
}

void ImageProvide::updateImage()
{
    update();
}

QPointF ImageProvide::imagePosition()
{
    return position();
}

void ImageProvide::setCurPosition(qreal zoomFactor, qreal x, qreal y)
{
    QPointF curPos(x, y);
    setPosition(position() - curPos*(zoomFactor - 1.0f));
    update();
}

void ImageProvide::setCurOutPosition(qreal zoomFactor, qreal x, qreal y)
{
    QPointF curPos(x, y);
    setPosition(position() + curPos*(1.0f - zoomFactor));
    update();
}

void ImageProvide::movePosition(qreal x, qreal y)
{
    setPosition(/*position() + */QPointF(x, y));
    update();
}

void ImageProvide::leftRightMirrored()
{    
    m_imageThumb = m_imageThumb.mirrored(true, false);
    setNewSize(m_imageThumb.width(), m_imageThumb.height());
}

void ImageProvide::upDownMirrored()
{
    m_imageThumb = m_imageThumb.mirrored(false, true);
    setNewSize(m_imageThumb.width(), m_imageThumb.height());
}
void ImageProvide::leftRotation()
{
   QTransform matrix;
   matrix.rotate(-90.0);
   m_imageThumb = m_imageThumb.transformed(matrix,Qt::FastTransformation);
   setNewSize(m_imageThumb.width(), m_imageThumb.height());
}

void ImageProvide::rightRotation()
{
    QTransform matrix;
    matrix.rotate(90.0);
    m_imageThumb = m_imageThumb.transformed(matrix,Qt::FastTransformation);
    setNewSize(m_imageThumb.width(), m_imageThumb.height());
}

void ImageProvide::paint(QPainter *painter)
{
    m_imageThumb = m_imageThumb.scaled(m_imageWidth, m_imageHeight, Qt::KeepAspectRatio);
    painter->setRenderHint(QPainter::Antialiasing, false);
    painter->drawPixmap(0, 0, QPixmap::fromImage(m_imageThumb));
}
